﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace InterfaceComponents
{
    /// <summary>
    /// This class implements a slider which can be used to control the volume of the game.
    /// </summary>
    public class Slider : InterfaceComponent, MenuComponent
    {
        private int value
        {
            get;
            set;
        }

        private Sirian.SirianGame.Volume volume;

        private SpriteFont spriteFont;

        private Texture2D barTexture;

        public Slider(int value, Vector2 position, SpriteBatch spriteBatch, SpriteFont spriteFont, Sirian.SirianGame.Volume volume)
        {
            this.value = value;
            this.position = position;
            this.spriteBatch = spriteBatch;
            this.spriteFont = spriteFont;
            this.volume = volume;
            //create red texture to display the sliderbar
            barTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            barTexture.SetData(new[] { Color.Red});
        }

        /// <summary>
        /// Draws the slider with the specified color.
        /// </summary>
        /// <param name="color">Color of the font.</param>
        public override void Draw(Color color)
        {
            Rectangle dest = new Rectangle((int)position.X + value, (int)position.Y, 5, (int)spriteFont.MeasureString("100%").Y);
            spriteBatch.Draw(barTexture, dest, color);
            spriteBatch.DrawString(spriteFont, value.ToString() + "%", new Vector2(position.X + 120, position.Y) - new Vector2(2, 0), Color.Black);
            spriteBatch.DrawString(spriteFont, value.ToString() + "%", new Vector2(position.X + 120, position.Y) + new Vector2(2, 0), Color.Black);
            spriteBatch.DrawString(spriteFont, value.ToString() + "%", new Vector2(position.X + 120, position.Y) - new Vector2(0, 2), Color.Black);
            spriteBatch.DrawString(spriteFont, value.ToString() + "%", new Vector2(position.X + 120, position.Y) + new Vector2(0, 2), Color.Black);
            spriteBatch.DrawString(spriteFont, value.ToString() + "%", new Vector2(position.X + 120, position.Y), color);
        }

        /// <summary>
        /// Checks if the slider is moved.
        /// </summary>
        /// <param name="selected">Whether the user selected the slider with the keyboard</param>
        public virtual void Update(KeyboardState keyboard, KeyboardState oldKeyboard, MouseState mouse, MouseState oldMouse, bool selected)
        {
            switch (volume)
            {
                case Sirian.SirianGame.Volume.Music:
                    value = GlobalGameClasses.GlobalGameValues.Values.volume_Music;
                    break;
                case Sirian.SirianGame.Volume.Sound:
                    value = GlobalGameClasses.GlobalGameValues.Values.volume_soundEffect;
                    break;
            }
            if (selected && KeyPressed(keyboard, oldKeyboard, Keys.Left))
            {
                if (value > 0)
                {
                    value -= 5;
                }
            }
            if (selected && KeyPressed(keyboard, oldKeyboard, Keys.Right))
            {
                if (value < 100)
                {
                    value += 5;
                }
            }
            switch (volume)
            {
                case Sirian.SirianGame.Volume.Music:
                    GlobalGameClasses.GlobalGameValues.Values.volume_Music = value;
                    break;
                case Sirian.SirianGame.Volume.Sound:
                    GlobalGameClasses.GlobalGameValues.Values.volume_soundEffect = value;
                    break;
            }
        }

        private bool KeyPressed(KeyboardState keyboard, KeyboardState oldKeyboard, Keys key)
        {
            return keyboard.IsKeyUp(key) && oldKeyboard.IsKeyDown(key);
        }

    }
}

